<template>
	<view class="all">
	<view class="zc">
		<view class="user">用 户 注 册</view>
		<view class="up">
			<input type="text" value="" placeholder="账户" v-model="Reguserdata.UserAccount" name="username" />
			<input type="password" value="" placeholder="密码" v-model="Reguserdata.password" name="password" />
			<input type="password" value="" placeholder="确认密码" v-model="passwordconfirm" name="password" />
			<view class="" style="display: flex; align-items: center;">
				<text style="width: 150upx; font-size: 45upx;color: #8BAA81;">头 像：</text>
				<view class="" style="width: 250upx; background-color: #cfd9c8;" @click="chooseImg">
					<image :src="TempuserImg.path" mode="" style="width: 250upx; height: 250upx;"></image>
				</view>
			</view>
			<view>
			<button class="anniu1" type="primary" @click="regist">注 册</button>
			<button class="anniu2" type="default"  @click="back">返 回</button>
			</view>
		</view>
	</view>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				tableData:[],	
				Reguserdata:{
					UserAccount:'',
					password:'',
					UserImg:''
				},
				TempuserImg:'',
				passwordconfirm:''
			}
		},
		onLoad() {
			this.getDatabase()  
		},
		methods:{
			chooseImg(){
				uni.chooseImage({
					count:1,
					success: (res) => {
						console.log(res.tempFilePaths[0])
						this.TempuserImg=res.tempFiles[0]
					}
				})
			},
			getDatabase:function(){
				let that=this
				const db = uniCloud.database()//获取数据库的引照
				db.collection('userInfo').get().then((res)=>{
					console.log('获取数据库：')
					console.log(res)
					that.tableData=res.result.data
				}).catch((err)=>{
					uni.showToast({
						title:err.message
					})
				})
			},
			back:function(){
				uni.reLaunch({
					url:"../index/index"
				})
			},	
			async regist(){
				if(this.Reguserdata.password!=this.passwordconfirm){
					uni.showModal({
						content:"两次输入密码不一致！！",
						showCancel:false
					})
					this.Reguserdata={}
					this.passwordconfirm=''
				}else{
					await uniCloud.uploadFile({
						cloudPath:this.TempuserImg.name,
						filePath:this.TempuserImg.path
					}).then(res=>{
						console.log(res)
						this.Reguserdata.UserImg=res.fileID
						})
						console.log(this.Reguserdata)
						uniCloud.callFunction({
							name:"Reg",
							data:this.Reguserdata,
							success: (res) => {
								console.log(res)
								uni.redirectTo({
									url:'../index/index'
								})
								uni.showToast({
									icon:"success",
									title:"注册成功！！！"
								})
							}
						})
					}
				}
			
		}
	}
</script>

<style>
	.all{
		display: flex;
		flex-direction: column;
		background-size: 100%;
	}
	.zc{
		height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 200upx;
	}
	.user{
		color: #848a82;
		font-size: 50upx;
		font-weight: bold;
		margin-bottom: 30upx;
		
	}
	input{
		background-color: #fdfbfb;
		border: 4upx solid #91aa77 ;
		height: 80upx;
		width: 500upx;
		border-radius: 30upx;
		padding-left: 20upx;
		margin-bottom: 20upx;
	}
	button{
		background-color: #7fcc76;
		margin-top: 20upx;
	}
	.anniu1{
		background-color: #8baa81;
	}
	.anniu2{
		color: #55557f;
		background-color: #afcc88;
	}
	
</style>
